/**
 * 公共场所信息JS
 * @author Assassin
 */
$(document).ready(function(){
    $('#uploadPictureLink').on('click',function(){
        $('#pictureFile').click();
    });
    $('#pictureImg').on('click',function(){
        $('#pictureFile').click();
    });
});

// 执行添加公共场所信息
function excuteAddPolicyInfo() {
    var title = $.trim($('#title').val());
    if(shiro.util.isEmpty(title)) {
        layer.tips('场所名称不能为空', '#title');
        return false;
    }
    var intro = $.trim($('#intro').val());
    if(shiro.util.isEmpty(intro)) {
        layer.tips('场所简介不能为空', '#intro');
        return false;
    }
    var address = $.trim($('#address').val());
    if(shiro.util.isEmpty(address)) {
        layer.tips('场所地址不能为空', '#address');
        return false;
    }
    var lng = $.trim($('#lng').val());
    if(shiro.util.isEmpty(lng)) {
        layer.tips('场所经度不能为空', '#lng');
        return false;
    }
    var lat = $.trim($('#lat').val());
    if(shiro.util.isEmpty(lat)) {
        layer.tips('场所纬度不能为空', '#lat');
        return false;
    }
    var placeDesc = CKEDITOR.instances.content_editor.getData();
    if(shiro.util.isEmpty(placeDesc)) {
        layer.msg('场所描述不能为空', {icon: 5,time: 1000});
        return false;
    }
    // 提交信息
    shiro.ajax({
        url: 'place/add',
        data:{'title': title,'intro':intro,'address':address,'lng':lng,'lat':lat, "placeDesc":placeDesc},
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "place/list";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
                $('#title').val('');
            }
        }
    });
    return false;
}

// 执行修改公共场所信息
function excuteEditPolicyInfo() {
    var title = $.trim($('#title').val());
    if(shiro.util.isEmpty(title)) {
        layer.tips('场所名称不能为空', '#title');
        return false;
    }
    var intro = $.trim($('#intro').val());
    if(shiro.util.isEmpty(intro)) {
        layer.tips('场所信息简介不能为空', '#intro');
        return false;
    }
    var address = $.trim($('#address').val());
    if(shiro.util.isEmpty(address)) {
        layer.tips('场所信息简介不能为空', '#address');
        return false;
    }
    var lng = $.trim($('#lng').val());
    if(shiro.util.isEmpty(lng)) {
        layer.tips('场所经度不能为空', '#lng');
        return false;
    }
    var lat = $.trim($('#lat').val());
    if(shiro.util.isEmpty(lat)) {
        layer.tips('场所纬度不能为空', '#lat');
        return false;
    }
    var placeDesc = CKEDITOR.instances.content_editor.getData();
    if(shiro.util.isEmpty(placeDesc)) {
        layer.msg('场所描述不能为空', {icon: 5,time: 1000});
        return false;
    }

    var placeId = $('#placeId').val();
    // 提交信息
    shiro.ajax({
        url: 'place/edit',
        data:{'placeId': placeId, 'title': title,'intro':intro,'address':address,'lng':lng,'lat':lat, "placeDesc":placeDesc},
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "place/list";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
                $('#title').val('');
            }
        }
    });
    return false;
}

// 删除公共场所
function deletePlace(placeId) {
    layer.msg('确定删除该公共场所？', {
        time: 0,
        btn: ['确定', '取消'],
        yes: function(index){
            layer.close(index);
            // 提交删除
            shiro.ajax({
                url: 'place/delete',
                data:{"placeId": placeId},
                success: function(data) {
                    if(data.result == 0) {
                        layer.msg(data.message, {icon: 1,time: 1000}, function() {
                            window.location.href = shiro.baseUrl + "place/list";
                        });
                    } else {
                        layer.msg(data.message, {icon: 5,time: 1000});
                    }
                }
            });
        }
    });
}

// 批量删除公共场所信息
function batchDeletePolicy() {
    var placeIdArray = shiro.form.getCheckBoxSelectValues('placeIdCheckBox');
    if(placeIdArray.length == 0) {
        layer.msg('请选择要操作的数据行', {icon: 5,time: 1000});
    } else {
        layer.msg('确定删除选中的公共场所信息？', {
            time: 0,
            btn: ['确定', '取消'],
            yes: function(index){
                layer.close(index);
                var placeIds = placeIdArray.join(',');
                // 提交删除
                shiro.ajax({
                    url: 'place/batchDelete',
                    data:{"placeIds": placeIds},
                    success: function(data) {
                        if(data.result == 0) {
                            layer.msg(data.message, {icon: 1,time: 1000}, function() {
                                window.location.href = shiro.baseUrl + "place/list";
                            });
                        } else {
                            layer.msg(data.message, {icon: 5,time: 1000});
                        }
                    }
                });
            }
        });
    }
}